<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <!-- 表单头部 -->
      <div class="login-header">
        <h2>SaaS需求云平台</h2>
        <p v-if="formType === 'login'">请输入您的账号和密码登录</p>
        <p v-else-if="formType === 'register'">填写以下信息完成注册</p>
        <p v-else>填写企业信息完成入驻</p>
      </div>
      
      <!-- 表单切换 -->
      <div class="form-tabs">
        <el-button 
          :type="formType === 'login' ? 'primary' : 'default'" 
          @click="switchForm('login')"
          class="tab-button"
        >
          用户登录
        </el-button>
        <el-button 
          :type="formType === 'register' ? 'primary' : 'default'" 
          @click="switchForm('register')"
          class="tab-button"
        >
          注册账号
        </el-button>
        <el-button 
          :type="formType === 'enterprise' ? 'primary' : 'default'" 
          @click="switchForm('enterprise')"
          class="tab-button"
        >
          企业入驻
        </el-button>
      </div>
      
      <!-- 登录表单 -->
      <el-form
        v-if="formType === 'login'"
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="auth-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名" 
            :prefix-icon="User"
            clearable
            autocomplete="on"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码" 
            :prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>
        
        <el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" class="remember-me">记住密码</el-checkbox>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="submit-button" 
            :loading="loading"
            native-type="submit"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <!-- 注册表单 -->
      <el-form
        v-if="formType === 'register'"
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerRules"
        class="auth-form"
        @submit.prevent="handleRegister"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="registerForm.username" 
            placeholder="请输入用户名" 
            :prefix-icon="User"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="registerForm.password" 
            type="password" 
            placeholder="请输入密码" 
            :prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input 
            v-model="registerForm.confirmPassword" 
            type="password" 
            placeholder="请确认密码" 
            :prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="phone">
          <el-input 
            v-model="registerForm.phone" 
            placeholder="请输入手机号" 
            :prefix-icon="Phone"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="email">
          <el-input 
            v-model="registerForm.email" 
            placeholder="请输入邮箱" 
            :prefix-icon="Postcard"
            clearable
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="submit-button" 
            :loading="loading"
            native-type="submit"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>
      
      <!-- 企业入驻表单 -->
      <el-form
        v-if="formType === 'enterprise'"
        ref="enterpriseFormRef"
        :model="enterpriseForm"
        :rules="enterpriseRules"
        class="auth-form"
        @submit.prevent="handleEnterpriseRegister"
      >
        <el-form-item prop="name">
          <el-input 
            v-model="enterpriseForm.name" 
            placeholder="请输入企业名称" 
            :prefix-icon="OfficeBuilding"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="description">
          <el-input 
            v-model="enterpriseForm.description" 
            placeholder="请输入企业描述（选填）" 
            type="textarea"
            :rows="3"
          />
        </el-form-item>
        
        <el-form-item prop="contactName">
          <el-input 
            v-model="enterpriseForm.contactName" 
            placeholder="请输入联系人姓名" 
            :prefix-icon="User"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="contactPhone">
          <el-input 
            v-model="enterpriseForm.contactPhone" 
            placeholder="请输入联系人电话" 
            :prefix-icon="Phone"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="contactEmail">
          <el-input 
            v-model="enterpriseForm.contactEmail" 
            placeholder="请输入联系人邮箱" 
            :prefix-icon="Postcard"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="address">
          <el-input 
            v-model="enterpriseForm.address" 
            placeholder="请输入企业地址" 
            :prefix-icon="Location"
            clearable
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="submit-button" 
            :loading="loading"
            native-type="submit"
          >
            提交入驻申请
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p>版本号：v1.0.0</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { User, Lock, Phone, Postcard, OfficeBuilding, Location } from '@element-plus/icons-vue'
import { useAuth } from '@/hooks/useAuth'

// 使用认证hook
const { 
  formType, 
  loading, 
  loginForm, 
  registerForm, 
  enterpriseForm, 
  loginRules, 
  registerRules, 
  enterpriseRules, 
  switchForm, 
  handleLogin, 
  handleRegister, 
  handleEnterpriseRegister, 
  initForm 
} = useAuth()

// 表单引用
const loginFormRef = ref(null)
const registerFormRef = ref(null)
const enterpriseFormRef = ref(null)

// 生命周期钩子 - 组件挂载时执行
onMounted(() => {
  initForm()
})
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-form-wrapper {
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 480px;
  transition: transform 0.3s ease;
}

.login-form-wrapper:hover {
  transform: translateY(-5px);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #303133;
  margin-bottom: 10px;
  font-size: 24px;
}

.login-header p {
  color: #909399;
  font-size: 14px;
}

/* 表单切换标签 */
.form-tabs {
  display: flex;
  justify-content: center;
  margin-bottom: 30px;
  gap: 10px;
}

.tab-button {
  flex: 1;
  max-width: 140px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.tab-button:hover {
  transform: translateY(-2px);
}

/* 通用表单样式 */
.auth-form {
  width: 100%;
}

.auth-form .el-form-item {
  margin-bottom: 20px;
}

.auth-form .el-input {
  height: 40px;
}

.auth-form .el-textarea {
  margin-bottom: -10px;
}

.auth-form .el-input__wrapper {
  border-radius: 8px;
}

.remember-me {
  font-size: 14px;
  color: #606266;
}

.submit-button {
  width: 100%;
  height: 40px;
  border-radius: 8px;
  font-size: 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.submit-button:hover {
  transform: scale(1.02);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.login-footer {
  text-align: center;
  margin-top: 20px;
}

.login-footer p {
  color: #909399;
  font-size: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-form-wrapper {
    padding: 30px 20px;
  }
  
  .login-header h2 {
    font-size: 20px;
  }
  
  .form-tabs {
    flex-direction: column;
    align-items: stretch;
  }
  
  .tab-button {
    max-width: none;
  }
}
</style>